package org.stan.beanvalidator.factory.validator;

import org.stan.beanvalidator.annotation.BeanValidate;
import org.stan.beanvalidator.bean.PatternField;
import org.stan.beanvalidator.factory.AnnotationFactoty;
import org.stan.beanvalidator.utils.ValidatorUtil;
import java.lang.reflect.Field;
import java.util.Set;

/**
 * 手机号校验器
 * @author src
 * @Date 2020/6/9 12:47
 * @version 1.0
 */
public class MobileValidator extends AnnotationFactoty {
    @Override
    public void beanValidate(Field field, Object bean) throws Exception {
        check(field, bean, null);
    }

    @Override
    public void beanValidate(Field field, Object bean, Set<String> excludes) throws Exception {
        check(field, bean, excludes);
    }

    @Override
    public void beanValidate(Object bean, Field field, Set<PatternField> patternFields) throws Exception {
        // 当前字段没有新的校验方式，照原来的校验方式校验
        if(!ValidatorUtil.isContains(field, patternFields)){
            check(field,bean,null);
            return;
        }
        // 有新的校验方式，按新的格式校验
        ValidatorUtil.validateNew(field, bean, patternFields);
    }

    @Override
    public void beanValidate(Field field, Object bean, Class<?> clazz, BeanValidate beanValidate) throws Exception {
        ValidatorUtil.validateAnnotation(beanValidate,field,bean,clazz);
        // 校验非空
        ValidatorUtil.validateMobile(ValidatorUtil.getAspectValue(field,clazz,bean),field);
    }

    private void check(Field field, Object bean, Set<String> excludes)throws Exception{
        if(null!=excludes&&excludes.size()>0&&excludes.contains(field.getName())){ return; }
        ValidatorUtil.validateMobile(ValidatorUtil.getValue(field,bean),field);
    }

}
